#include "LineRenderer.h"
#include "2DDevice.h"
#include "Window.h"
#include <d3dx9.h>
#include "Map.h"
#include "Main.h"
cLineRenderer::cLineRenderer(cMain & Main): Main(Main)
{
	g_pVB = NULL;
	NumLinesToRender = 0;
	Restart();
}


cLineRenderer::~cLineRenderer()
{

}

void cLineRenderer::Restart()
{
	NumLinesToRender = 0;
    if( g_pVB != NULL )
	{
        g_pVB->Release();
		g_pVB = NULL;
	}

	if( FAILED( Main.GetTwoDDevice().GetDevice()->CreateVertexBuffer(MAX_LINES_TO_RENDER * NUM_VERTICES_IN_LINE * sizeof(CUSTOMVERTEX),
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
    {
		MessageBox(0, "cLineRenderer::CreateVertexBuffer failed", WINDOW_CAPTION,MB_OK); 
        return ;
    }
}



void cLineRenderer::AddLineToRender(D3DXVECTOR3 StartPos, D3DXVECTOR3 EndPos, DWORD color, float LWidth)
{
   // memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
	Lines[NumLinesToRender].StartPos = StartPos;
	Lines[NumLinesToRender].EndPos = EndPos;
	Lines[NumLinesToRender].color = color;
	Lines[NumLinesToRender].LWidth = LWidth;
	NumLinesToRender++;
}

void cLineRenderer::Render()
{
	D3DXVECTOR3 V1, V2, V3, V4, DirVector , CrossVector, UnitDir;
	float VecLengh;
	int i;
	float LWidth;
 CUSTOMVERTEX* pVertices;
    if( FAILED( g_pVB->Lock( 0, NUM_VERTICES_IN_LINE * sizeof(CUSTOMVERTEX), (void**)&pVertices, 0 ) ) )
	{
		MessageBox(0, "cLineRenderer::g_pVB->Lock failed", WINDOW_CAPTION,MB_OK); 
        return ;
	}
			for(i=0; i!= NumLinesToRender; i++)
			{
				LWidth = Lines[i].LWidth;
				
				DirVector = (Main.GetMap().GetStartPos() + Lines[i].EndPos) - (Main.GetMap().GetStartPos() + Lines[i].StartPos);
				VecLengh = D3DXVec3Length(&DirVector);
				UnitDir = DirVector / VecLengh;
				CrossVector = D3DXVECTOR3(-UnitDir.y, UnitDir.x, 0.0f);
				
				V1 = Main.GetMap().GetStartPos() + Lines[i].StartPos - (CrossVector * Lines[i].LWidth);
				V2 = Main.GetMap().GetStartPos() + Lines[i].StartPos + (CrossVector * Lines[i].LWidth);
				V3 = Main.GetMap().GetStartPos() + Lines[i].EndPos + (CrossVector * Lines[i].LWidth);
				V4 = Main.GetMap().GetStartPos() + Lines[i].EndPos - (CrossVector * Lines[i].LWidth);
				
				pVertices[ 6*i   ].pos = V1;
				pVertices[(6*i)+1].pos = V2;
				pVertices[(6*i)+2].pos = V3;

				pVertices[(6*i)+3].pos = V1;
				pVertices[(6*i)+4].pos = V3;
				pVertices[(6*i)+5].pos = V4;
				
				pVertices[(6*i)  ].color = Lines[i].color;
				pVertices[(6*i)+1].color = Lines[i].color;
				pVertices[(6*i)+2].color = Lines[i].color;
				pVertices[(6*i)+3].color = Lines[i].color;
				pVertices[(6*i)+4].color = Lines[i].color;
				pVertices[(6*i)+5].color = Lines[i].color;
			}
	g_pVB->Unlock();
	Main.GetTwoDDevice().GetDevice()->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
    
	D3DXMATRIX matWorld;
	D3DXMatrixIdentity(&matWorld);
    Main.GetTwoDDevice().GetDevice()->SetTransform( D3DTS_WORLD, &matWorld );

    
    D3DXMATRIX matView;
    D3DXMatrixIdentity(&matView);
    Main.GetTwoDDevice().GetDevice()->SetTransform( D3DTS_VIEW, &matView );
	
	Main.GetTwoDDevice().GetDevice()->DrawPrimitive( D3DPT_TRIANGLELIST, 0,  NumLinesToRender * NUM_TRIANGLES_IN_LINE);

	NumLinesToRender = 0;
}